<?php
    class Admin_CommentController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        
        public function init() {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admin_Model_Comment();
        }
        
        public function indexAction() {
            $this->view->headTitle('Quản lý bình luận tour');
            
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteComments($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $listComment = $this->_model->listComment();
            $adapter = new Zend_Paginator_Adapter_DbSelect($listComment);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(20);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function editAction() {
            $this->view->headTitle('Sửa bình luận tour');
            $comment_id = $this->_arrParam['comment_id'];
            $this->view->data = $this->_model->getCommentById($comment_id);
            
            if($this->_request->isPost()) {
                $term = $this->_arrParam['term'];
                $status = $this->_arrParam['status'];
                
                $validate = new Admin_Form_ValidateComment();
                if(!$validate->validateTerm($term)) {
                    $this->view->errorTerm = 'Nội dung không được để trống';
                }
                else {
                    $data = array(
                        'term' => $term,
                        'status' => $status,
                    );
                    $this->_model->editComment($comment_id, $data);
                    $this->_redirect('admin/comment');
                }
            }
        }
        
        public function deleteAction() {
            $comment_id = (int) $this->_arrParam['comment_id'];
            $this->_model->deleteComment($comment_id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
    }
